import pymysql

class DBTools(object):
    @classmethod
    def __get_conn(cls):
        conn =pymysql.connect(host="211.103.136.244",port=7061,user="student",
                       password="iHRM_student_2021",database="test_db",charset='utf8')
        return conn
    @classmethod
    def select_one(cls, sql):
        conn = None
        cursor = None
        res = None
        try:
            conn = cls.__get_conn()
            cursor = conn.cursor()
            cursor.execute(sql)
            res = cursor.fetchone()
        except Exception as e:
            print(e)
        finally:
            cursor.close()
            conn.close()
            return res
    # @classmethod
    # def select_many(cls):
    #     pass
    @classmethod
    def uid_db(cls,sql):
        conn = None
        cursor = None
        try:
            conn = cls.__get_conn()
            cursor = conn.cursor()
            cursor.execute(sql)
            conn.commit()
            print("提交事务管理")
            print("影响行数：", conn.affected_rows())
        except Exception as e:
            print(e)
        finally:
            conn.rollback()
            print("事务回滚")
            cursor.close()
            conn.close()